package com.sjtu.rental.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sjtu.rental.common.PaginationSupport;
import com.sjtu.rental.entity.House;
import com.sjtu.rental.form.SearchForm;
import com.sjtu.rental.services.HouseService;

@Controller
@RequestMapping("/search")
public class SearchController {

	@Autowired
	private HouseService houseService;

	@SuppressWarnings("unchecked")
	@RequestMapping
	public String search(
			@ModelAttribute("searchForm") SearchForm searchForm,
			@RequestParam(value = "page", required = false, defaultValue = "0") String page,
			Model model) {
		PaginationSupport p = houseService.getHouseBySearchForm(
				searchForm, Integer.parseInt(page));
		List<House> houses = (List<House>) p.getItems();
		model.addAttribute("p", p);
		model.addAttribute("houses", houses);
		//最近成交
		List<House> dealHouses = houseService.getDealHouses();
		model.addAttribute("dealHouses", dealHouses);
		//热门预约房源
		List<House> hotHouses = houseService.getHotHouses();
		model.addAttribute("hotHouses", hotHouses);
		//今天发布的房源数
		int todayCount = houseService.getTodayHouseCount();
		model.addAttribute("todayCount", todayCount);
		return "searchResult";
	}
}
